'use strict';
const Service = require('egg').Service;
const Controller = require('egg').Controller;
const svgCaptcha = require('svg-captcha')
class HomeController extends Controller {
    async index() {
        const { ctx } = this;
        ctx.body = 'hi, egg';
    }

    async login() {
            const { ctx } = this;
            ctx.body = 'hi, egg';
        }
        /* get */
        /* szk入口登录判断 */ 
    async login1() {
            const { ctx } = this;
            var req = this.ctx.request.query
            var sql = `SELECT * from user WHERE uid=${req.uid}`;
            ctx.body = await this.app.mysql.query(sql);
        }
        /* post接口 */
    async register() {
        const { ctx } = this;
        var req = this.ctx.request.body
            /*  console.log(req); */

        var sql2 = `SELECT * from user WHERE tel="${req.tel}" || email="${req.youxiang}"`;
        var sqldata2 = await this.app.mysql.query(sql2);
        if (sqldata2[0] != undefined) {
            ctx.body = { 'retu': sqldata2, errinfo: "账号已被注册", code: "4003" };
        } else {
            /* 操作sql */
            var sql = `INSERT  INTO user (touxiang, email, tel,password) VALUES('${req.touxiang}', '${req.youxiang}', "${req.tel}","${req.mima}")`;
            var sqldata = await this.app.mysql.query(sql);
            ctx.body = { 'retu': sqldata, code: "2002" };
        }

    }
    async register1() {
            const { ctx } = this;
            ctx.body = 'hi, egg';
        }
        /* post */
    async verify() {
            var req = this.ctx.request.body
            var verifytxt = this.ctx.session.veriftext.toUpperCase()
                /* 拿到用户输入信息进行数据库匹配 */
            if (req.verifytext.toUpperCase() == verifytxt) {
                var sql1 = `SELECT * from user WHERE tel="${req.zhanghao}" || email="${req.zhanghao}"`;
                var res1 = await this.app.mysql.query(sql1)
                this.ctx.body = { res1, verify: "验证码通过", code: 2003 }
            } else {
                this.ctx.body = { verify: "验证码错误", code: "4004" }
            }

        }
        /* get */
    async verify1() {
        /* 验证码部分 */
        const captcha = svgCaptcha.create({
                size: 4,
                fontSize: 45,
                width: 100,
                height: 50,
                background: '#CDECF9'
            })
            //将验证码缓存
        this.ctx.session.veriftext = captcha.text
            //将验证码svg字符发给前端显示
        this.ctx.body = captcha.data

    }





    async homedata(){
        let ziduan=this.ctx.request.query
        ziduan.start=ziduan.start||0
        ziduan.count=ziduan.count||80
        let sql=`select * from goods limit ${ziduan.start},${ziduan.count}`
        let res=await this.ctx.app.mysql.query(sql)
        this.ctx.body=res
    }
    async homedata1(){
        let ziduan=this.ctx.request.query
        ziduan.start=ziduan.start||0
        ziduan.count=ziduan.count||100
        
        let sql=`select * from goods where id=${ziduan.id}`
        let res=await this.ctx.app.mysql.query(sql)
        this.ctx.body=res
    }
    async detail(){
        let ziduan=this.ctx.request.query
        console.log(ziduan);
        let sql=`select * from detail where id=${ziduan.id}`
        let res=await this.ctx.app.mysql.query(sql)
        console.log(res);
        this.ctx.body={info:res[0],code:204}
    }
    async report(){
        let ziduan=this.ctx.request.query
        console.log(ziduan);
        // let sql=`select * from report where id=${ziduan.id}`
        let sql=`SELECT * FROM report INNER JOIN user WHERE user.uid=report.uid AND report.id=${ziduan.id}`
        let res=await this.ctx.app.mysql.query(sql)
        this.ctx.body={info:res,code:205}
    }

    async setreport(){
        // let ziduan=this.ctx.request.query
        // console.log(ziduan);
        // let user=ziduan.user
        // let reportmsg=ziduan.reportmsg
        // let sql=`INSERT INTO report (uid,id,content) VALUES (${user.userid},${ziduan.id},"${reportmsg}")`
        // let res=await this.ctx.app.mysql.query(sql)
        // this.ctx.body={info:res,code:206}

        let ziduan=this.ctx.request.query
        console.log(ziduan);
        // let user=ziduan.user
        let reportmsg=ziduan.reportmsg
        let sql=`INSERT INTO report (id,content) VALUES (${ziduan.id},"${reportmsg}")`
        let res=await this.ctx.app.mysql.query(sql)
        this.ctx.body={info:res,code:206}
    }


    //root管理用户
    async userfrom(){
        let sql=`select * from user`
        let res=await this.ctx.app.mysql.query(sql)
        this.ctx.body={info:res,code:207}
    }
}

module.exports = HomeController;